package query.model;

import java.util.ArrayList;
import java.util.List;

import org.tartarus.snowball.ext.FrenchStemmer;

import stemming.SnowBallStemmer;
import utils.StopList;

/**
 * The class that represent a query.
 */
public class UserQuery
{
  /** The initial query. */
  private String initialQuery;
  
  /** The query words. */
  private List<QueryWord> queryWords;

  /** The query name. */
  private String queryName;

  /**
   * Instantiates a new user query.
   *
   * @param queryName the query
   * @param queryTerms 
   */
  public UserQuery(String queryName, String queryTerms)
  {
    this.queryName = queryName;
    this.initialQuery = queryTerms;
    queryWords = parseQueryWords();
  }
  
  private List<QueryWord> parseQueryWords()
  {
    List<QueryWord> result = new ArrayList<>();
    String[] words = initialQuery.split("[^\\p{L}\\p{Nd}]");
    SnowBallStemmer stemmer = new SnowBallStemmer(new FrenchStemmer());
    StopList stopList = new StopList();
    for (int i = 0; i < words.length; i++)
    {
      String string = words[i];
      string = string.replaceAll("\\s+", "");
      String stemmedString = stemmer.stemAWord(string);
      
      if(!stopList.contains(stemmedString) && !stemmedString.equals(""))
      {
        result.add(new QueryWord(i, stemmedString, 5.0));
      }
    }
    return result;
  }
  
  public void addQueryWord(QueryWord word)
  {
    queryWords.add(word);
  }

  /**
   * Gets the query.
   *
   * @return the query
   */
  public List<QueryWord> getQueryWords()
  {
    return queryWords;
  }

  /**
   * Gets the query name.
   *
   * @return the query name
   */
  public String getQueryName()
  {
    return queryName;
  }
  
  
  @Override
  public String toString()
  {
    return queryWords.toString();
  }
}
